<template>
    <div calss="top">
        <div class="is-background">
            <el-image
                    style="width: 100%; height: 100%"
                    :src="url"></el-image>
        </div>
        <div class="font">
            <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
                <h3 class="login-title">欢迎登录问卷系统</h3>
                <el-form-item label="账号" prop="username">
                    <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
                </el-form-item>
                <el-form-item>
                    <el-button class="button-login" type="primary" round v-on:click="onSubmit('loginForm')">登录</el-button>
                    <el-button class="button-register" type="primary" round v-on:click="onSubmit()">注册</el-button>
                </el-form-item>
            </el-form>

            <el-dialog
                title="温馨提示"
                :visible.sync="dialogVisible"
                width="30%"
                >
            <span>请输入账号和密码</span>
            <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
        </el-dialog>

            <el-dialog
                    title="温馨提示"
                    :visible.sync="errDialogVisible"
                    width="30%"
                    >
                <span>账号或者密码错误</span>
                <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="errDialogVisible = false">确 定</el-button>
      </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                form: {
                    username: '',
                    password: ''
                },

                userInfo: {
                    id: '',
                    username: '',
                    password: '',
                    avatar: '',
                },
                loginUsername: '',
                loginPassword: '',
                loginId: '',
                avatar: '',

                // 表单验证，需要在 el-form-item 元素中增加 prop 属性
                rules: {
                    username: [
                        {required: true, message: '账号不可为空', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '密码不可为空', trigger: 'blur'}
                    ]
                },

                // 对话框显示和隐藏
                dialogVisible: false,
                errDialogVisible: false,
                url:'https://image.wjx.com/images/newimg/register-login/bacg.jpg'
            }
        },
        methods: {

            // loginOnSubmit(formName){
            //     this.getUserInfo().then(function () {
            //         this. onSubmit(formName);
            //     })
            // },

            async getUserInfo(params) {
                let _this = this;
                let isSuccess = false;
                await this.axios({
                    url: "http://localhost:8088/verifyLogin",
                    headers: {
                        'Content-Type': 'application/json;charset=utf-8'
                    },
                    method: "post",
                    params: {
                        'userName': _this.form.username
                    }
                }).then(function (resp) {
                    _this.userInfo = resp.data;
                    _this.loginUsername = resp.data.username;
                    _this.loginPassword = resp.data.password;
                    _this.loginId = resp.data.id;
                    _this.avatar = resp.data.avatar;
                    isSuccess = true;
                });
                return isSuccess;
            },

            onSubmit(formName) {
                let _this = this;
                this.getUserInfo(_this.form.username).then(function (result) {
                    if (result) {
                        // do sth.
                        _this.$refs[formName].validate((valid) => {
                            if (valid) {
                                console.log(_this.form.username);
                                if(_this.form.username === _this.loginUsername
                                    && _this.form.password === _this.loginPassword){
                                    console.log(_this.userInfo);
                                    sessionStorage.setItem("token", 'true');
                                    sessionStorage.setItem("username", _this.loginUsername);
                                    sessionStorage.setItem("password", _this.loginPassword);
                                    sessionStorage.setItem("id", _this.loginId);
                                    _this.$router.push({path: "/index"});
                                    // 使用 vue-router 路由到指定页面，该方式称之为编程式导航
                                    //this.$router.push("/main");
                                } else {
                                    this.errDialogVisible = true;
                                    return false;
                                }
                            } else {
                                this.dialogVisible = true;
                                return false;
                            }
                        });
                    } else {
                        // do other sth.
                    }
                })
                // 为表单绑定验证功能
            }

        }
    }
</script>

<style lang="scss" scoped>

    .is-background {
        padding: 0px 0px 0px 0px;
        width:100%;
        height:100%;  /**宽高100%是为了图片铺满屏幕 */
        z-index:-1;
        position: absolute;
        top: 0px;  // 这里是设置与顶部的距离
        left: 0px;  // 这里是设置与左边的距离
    }

    .font{
        height: 305px;
        margin-top: 9%;
        margin-left: calc(calc(100vw - 400px)/2);
        background-color: rgba(255, 255, 255, 0);
        z-index:1;
        position: absolute;
    }

    .button-login{
        margin-right: 60px;
    }

    .button-register{
        margin-right: 40px;
    }

    .login-box {
        border: 1px solid #DCDFE6;
        width: 350px;
        padding: 40px 45px 15px 25px;
        border-radius: 15px;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        box-shadow: 0 0 25px #909399;
    }

    .login-title {
        text-align: center;
        margin: 0 auto 40px auto;
        color: #303133;
    }
</style>
